package Lee_code;

import java.util.LinkedList;
import java.util.List;

/**
 * @author blh
 * @date 2024年06月26日 15:28
 */
public class Lee_93 {
    static List<String> res = new LinkedList<>();
    int len;

    public List<String> restoreIpAddresses(String s) {
        len = s.length();
        backTrace(new LinkedList<>(), s);
        return res;
    }

    static void backTrace(LinkedList<String> string, String s) {
        int curLen = countLen(string);
        if (s.length() == curLen && string.size()==4) {
            res.add(getString(string));
            return;
        }
        if (string.size()>4){
            return;
        }

        for (int i = curLen; i < curLen + 3 && i < s.length(); i++) {
            String temp = s.substring(curLen, i+1);
            // 判断是不是以0开头
            if (temp.length() > 1 && temp.charAt(0) == '0') {
                continue;
            }
            // 判断是不是大于255
            if (Integer.parseInt(temp) > 255) {
                continue;
            }
            // 合乎规范的temp
            string.add(temp);
            backTrace(string, s);
            string.remove(string.size()-1);
        }
    }

    static private int countLen(LinkedList<String> string) {
        int count = 0;
        for (String s : string) {
            count = count + s.length();
        }
        return count;
    }

    static private String getString(LinkedList<String> string) {
        StringBuilder sb = new StringBuilder();
        for (String s : string) {
            sb.append(s).append(".");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    public static void main(String[] args) {
        String s = "101023";
        backTrace(new LinkedList<>(), s);
        System.out.println(res);
    }
}
